<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Faculty extends CI_Controller {

	public function __construct() {
		parent::__construct();
		if ($this->session->userdata('user')->site_role != 'faculty')
			redirect('/');

		if ($this->session->userdata('user')->wizard_status == 'active')
			redirect('new_user');
	}

	public function index() {
		$this->load->model('rank_model', '', TRUE);
		$this->load->model('event_model', '', TRUE);
		$this->load->model('company_model', '', TRUE);
		$data['overallRank'] = $this->rank_model->get_overall_rank($this->session->userdata('user')->user_id);
		$data['groupRank'] = $this->rank_model->get_group_rank($this->session->userdata('user')->user_id, 'score');
		$data['events'] = $this->event_model->get_user_events($this->session->userdata('user')->user_id);
		$data['companies'] = $this->company_model->get_user_companies($this->session->userdata('user')->user_id);
		$this->load->view('master', $data);
	}

	public function groups() {
		$this->load->model('rank_model', '', TRUE);
		$data['overallRank'] = $this->rank_model->get_overall_rank($this->session->userdata('user')->user_id);
		$data['groupRank'] = $this->rank_model->get_group_rank($this->session->userdata('user')->user_id);
		$this->load->view('master', $data);
	}

	public function group_search() {
		if (!$this->input->post()) redirect('/faculty');

		$this->load->model('group_model', '', TRUE);
		$data['groupList'] = $this->group_model->search_groups($this->input->post('searchTerm'));
		$this->load->view('master', $data);
	}

	public function group_details($groupType, $groupID) {
		$userID = $this->session->userdata('user')->user_id;
		$this->load->model('group_model', '', TRUE);
		$this->load->model('event_model', '', TRUE);
		$data['group'] = $this->group_model->get_group_details($groupType, $groupID);
		$data['officers'] = $this->group_model->get_group_officers($groupType, $groupID);
		$data['members'] = $this->group_model->get_top_ten($groupType, $groupID);
		$data['events'] = $this->event_model->get_group_events($groupID);
		$data['is_owner'] = $this->group_model->is_group_owner($groupType, $groupID, $userID);
		$data['groupID'] = $groupID;		// probably a better way to do this with url segments
		$this->load->view('master', $data);
	}

	public function events() {
		$this->load->model('event_model', '', TRUE);
		$data['events'] = $this->event_model->get_user_events($this->session->userdata('user')->user_id);
		$this->load->view('master', $data);		
	}

	public function event_search() {
		if (!$this->input->post()) redirect('/faculty');

		$this->load->model('event_model', '', TRUE);
		$data['eventList'] = $this->event_model->search_events($this->input->post('searchTerm'));
		$this->load->view('master', $data);
	}

	public function event_details($eventID) {
		$this->load->model('event_model', '', TRUE);
		$data['event'] = $this->event_model->get_event_details($eventID);
		//$data['members'] = $this->event_model->get_checkedin($eventID);
		$this->load->view('master', $data);
	}

	public function create_event() {
		$this->load->model('event_model', '', TRUE);

		$data = array(
			'event_name' => $this->input->post('event_name'),
			'event_description' => $this->input->post('event_description'),
			'location_name'=> $this->input->post('location_name'),
			'address1'=> $this->input->post('address1'),
			'address2'=> $this->input->post('address2'),
			'address3'=> $this->input->post('address3'),
			'city'=> $this->input->post('city'),
			'state'=> $this->input->post('state'),
			'zip'=> $this->input->post('zip'),
			'start_date'=> $this->input->post('start_date'),
			'end_date'=> $this->input->post('end_date'),
			'start_time'=> $this->input->post('start_time'),
			'end_time'=> $this->input->post('end_time'),
			'event_type'=> $this->input->post('event_type'),
			'event_cost'=> $this->input->post('event_cost'),
			'event_group'=> $this->input->post('event_group'),
		);	

		if ($this->event_model->insert_event($data)) {
			$this->load->view('master');
		}
		else
		$this->session->set_flashdata('error', 'There was an error saving the event information.  Please contact the system administrator.');

		$this->load->view('master');
	}

	public function update_event() {
		$this->load->model('event_model', '', TRUE);

		$data = array(
			'event_name' => $this->input->post('event_name'),
			'event_description' => $this->input->post('event_description'),
			'location_name'=> $this->input->post('location_name'),
			'address1'=> $this->input->post('address1'),
			'address2'=> $this->input->post('address2'),
			'address3'=> $this->input->post('address3'),
			'city'=> $this->input->post('city'),
			'state'=> $this->input->post('state'),
			'zip'=> $this->input->post('zip'),
			'start_date'=> $this->input->post('start_date'),
			'end_date'=> $this->input->post('end_date'),
			'start_time'=> $this->input->post('start_time'),
			'end_time'=> $this->input->post('end_time'),
			'event_type'=> $this->input->post('event_type'),
			'event_cost'=> $this->input->post('event_cost'),
			'event_group'=> $this->input->post('event_group'),
		);	

		if ($this->event_model->update_event($data)) {
			$this->load->view('master');
		}
		else
		$this->session->set_flashdata('error', 'There was an error saving the event information.  Please contact the system administrator.');

		$this->load->view('master');
	}

	public function companies() {
		$this->load->model('company_model', '', TRUE);
		$data['companies'] = $this->company_model->get_user_companies($this->session->userdata('user')->user_id);
		$this->load->view('master', $data);
	}

	public function company_search() {
		if (!$this->input->post()) redirect('/faculty');

		$this->load->model('company_model', '', TRUE);
		$data['companies'] = $this->company_model->search_companies($this->input->post('searchTerm'));
		$this->load->view('master', $data);
	}

	public function company_details($companyID) {
		$this->load->model('company_model', '', TRUE);
		$data['company'] = $this->company_model->get_company_details($companyID);
		$this->load->view('master', $data);
	}

	public function profile() {
		$this->load->model('user_model', '', TRUE);
		$data['user'] = $this->user_model->get_user_details($this->session->userdata('user')->user_id);
		$this->load->view('master', $data);
	}

	public function create_group() {
		$this->load->model('group_model', '', TRUE);
		$data['group'] = $this->group_model->getLastInserted();
		$this->load->view('master');
	}

	public function edit_officers($groupType,$groupID) {
		$this->load->model('group_model', '', TRUE);
		// only group owners can edit officers
		$userID = $this->session->userdata('user')->user_id;
		$is_owner = $this->group_model->is_group_owner($groupType, $groupID, $userID);
		if(!$is_owner){
			redirect('/');
		}
		$data['group'] = $this->group_model->get_group_details($groupType, $groupID);
		$data['members'] = $this->group_model->get_all_members($groupID);
		$officers = $this->group_model->get_group_officers($groupType, $groupID);
		$officerIDs = array();
		foreach($officers as $officer) {
			$officerIDs[] = $officer['user_id'];
		}
		$data['officerIDs'] = $officerIDs;
		$this->load->view('master', $data);
	}

}